package org.siming.generator.processors;

import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.Map;

/**
 * @Author: yesiming
 * @Platform: Mac
 * @Date: 11:51 上午 2021/5/19
 */
public abstract class AbstractAnnotationProcessor extends BaseAnnotationProcessor {

    public AbstractAnnotationProcessor() {
        System.out.println("init constructor");
        annotationProcessorSet.add(this);
    }

    @Override
    public void process(TopLevelClass topLevelClass) throws Exception {

        // 保存的内容是key-value列表
        Map<String, String> annotationMap = doResource();
        doProcess(topLevelClass, annotationMap);
    }

    protected void doProcess(TopLevelClass topLevelClass, Map<String, String> annotationMap) {
        for (Map.Entry<String, String> entry : annotationMap.entrySet()) {
            if (!this.getClass().getName().equalsIgnoreCase(entry.getKey())) {
                continue;
            }

            String annotations = entry.getValue();
            String[] annotationArr = annotations.split("\\,");
            for (String annotation : annotationArr) {
                String s = annotation.trim();
                String annotationName = fetchAnnotationName(s);
                try {
                    Class.forName(s);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    System.err.println("配置文件中内容：[" + s + "]未引入工程!");
                    break;
                }
                topLevelClass.addAnnotation(annotationName);
                topLevelClass.addImportedType(s);
            }

        }
    }


}
